import 'package:xindong/common/xindong_api_path.dart';
import 'package:xindong/manager/network/xindong_networking.dart';
import 'package:xindong/manager/network/xindong_response.dart';
import 'package:xindong/manager/xindong_user_controller.dart';
import 'package:xindong/models/xindong_auth_class.dart';
import 'package:xindong/models/xindong_money_class.dart';
import 'package:xindong/models/xindong_push_class.dart';
import 'package:xindong/models/user/xindong_wode_user.dart';
import 'package:xindong/models/user/xindong_user_config.dart';

class AttentionType {
  static const int follow = 1;
  static const int fans = 2;
  static const int friend = 3;
}

class VisitType {
  static const int visitMe = 1;
  static const int mineVisit = 2;
}

class XinDongUserHandle {
  // 请求加载列表
  //
  // yemian 分页（从0开始）
  // yemianSize 分页大小
  // hot 推荐
  // fate 今日缘分
  void requestHomeUserList(
      {required int yemian, int yemianSize = 20, int hot = 0, int fate = 0, required Function(NetworkResponse) callback}) {
    var parameters = {
      "page": yemian,
      "pagesize": yemianSize,
      "longitude": 0,
      "latitude": 0,
      "hot": hot,
      "fate": fate,
    };
    XinDongNetworking.instance
        .request(XinDongApi.userList, method: XinDongHttpMethod.post, parameters: parameters)
        .then(callback);
  }

  // 请求加载列表
  //
  // yemian 分页（从0开始）
  // yemianSize 分页大小
  // video_supei 推荐
  // voice_supei 今日缘分
  void requestMatchUserList(
      {required int yemian, int yemianSize = 20, int hot = 0, int fate = 0, required Function(NetworkResponse) callback}) {
    var parameters = {
      "page": yemian,
      "pagesize": yemianSize,
      "longitude": 0,
      "latitude": 0,
      "video_supei": true,
      "voice_supei": false,
    };
    XinDongNetworking.instance
        .request(XinDongApi.userList, method: XinDongHttpMethod.post, parameters: parameters)
        .then(callback);
  }

  // 请求加载列表
  //
  // yemian 分页（从0开始）
  // yemianSize 分页大小
  void requestAttentionUserList(
      {required int yemian,
      int yemianSize = 20,
      int type = AttentionType.follow,
      required Function(NetworkResponse) callback}) {
    var parameters = {"page": yemian, "pagesize": yemianSize, "types": type};
    XinDongNetworking.instance
        .request(XinDongApi.attentionList, method: XinDongHttpMethod.post, parameters: parameters)
        .then(callback);
  }

  // 请求加载列表
  //
  // yemian 分页（从0开始）
  // yemianSize 分页大小
  void requestVisitUserList(
      {required int yemian,
      int yemianSize = 20,
      int type = VisitType.visitMe,
      required Function(NetworkResponse) callback}) {
    var parameters = {"page": yemian, "pagesize": yemianSize, "types": type};
    XinDongNetworking.instance
        .request(XinDongApi.visitList, method: XinDongHttpMethod.post, parameters: parameters)
        .then(callback);
  }

  // 用户信息
  // userId 要查看的用户ID
  Future<Map<String, dynamic>?> userInfo({required dynamic userId}) async {
    var parameters = {};
    if ("$userId" == XinDongUserController.to.loginId) {
      parameters = {"longitude": 0, "latitude": 0};
    } else {
      parameters = {
        "user_id": userId,
        "to_user": XinDongUserController.to.loginId,
        "longitude": 0,
        "latitude": 0,
      };
    }
    final object = await XinDongNetworking.instance.request(
      XinDongApi.userInfo,
      method: XinDongHttpMethod.post,
      parameters: parameters,
    );
    if (object.code == XinDongServerStatus.success) {
      if (object.result is List) {
        return Map<String, dynamic>.from({});
      }
      return object.result;
    }
    return null;
  }

  // 用户配置
  // userId 要查看的用户ID
  Future<XinDongUserConfig?> userConfig({required dynamic userId}) async {
    final object = await XinDongNetworking.instance.request(
      XinDongApi.userConfig,
      method: XinDongHttpMethod.post,
      parameters: {"user_id": userId},
    );
    if (object.code == XinDongServerStatus.success) {
      return XinDongUserConfig.fromJson(object.data);
    }
    return null;
  }

  // 用户配置编辑
  Future<bool> userConfigEdit({required String key, required dynamic value}) async {
    final object = await XinDongNetworking.instance.request(
      XinDongApi.userConfigEdit,
      method: XinDongHttpMethod.post,
      parameters: {key: value},
    );
    return object.code == XinDongServerStatus.success;
  }

  // 用户配置
  Future<List<XinDongMoneyClass>> userConfigEditList({required int type}) async {
    final object = await XinDongNetworking.instance.request(
      XinDongApi.userConfigEditList,
      method: XinDongHttpMethod.post,
      parameters: {"types": type},
    );
    if (object.code == XinDongServerStatus.success) {
      List list = object.data;
      return list.map((e) => XinDongMoneyClass.fromJson(e)).toList();
    }
    return [];
  }

  // 用户配置
  Future<List<XinDongPushClass>> userPushList() async {
    final object = await XinDongNetworking.instance.request(
      XinDongApi.userPushList,
      method: XinDongHttpMethod.post,
    );
    if (object.code == XinDongServerStatus.success) {
      List list = object.data;
      return list.map((e) => XinDongPushClass.fromJson(e)).toList();
    }
    return [];
  }

  // 用户配置编辑
  Future<bool> userPushEdit({required dynamic id}) async {
    final object = await XinDongNetworking.instance.request(
      XinDongApi.userPushEdit,
      method: XinDongHttpMethod.post,
      parameters: {"id": id},
    );
    return object.code == XinDongServerStatus.success;
  }

  // 用户短视频列表
  // userId 要查看的用户ID
  Future<List<XinDongAuthClass>> userVideoList({required dynamic userId}) async {
    final object = await XinDongNetworking.instance.request(
      XinDongApi.userVideoList,
      method: XinDongHttpMethod.post,
      parameters: {"touser": userId, "page": 0},
    );
    // if (object.code == XinDongServerStatus.success) {
    //   List list = object.data;
    //   return list.map((e) => XinDongAuthClass.fromJson(e)).toList();
    // }
    return [];
  }

  // 用户认证列表
  // userId 要查看的用户ID
  Future<List<XinDongAuthClass>> userAuthList({required dynamic userId}) async {
    final object = await XinDongNetworking.instance.request(
      XinDongApi.userAuthList,
      method: XinDongHttpMethod.post,
      parameters: {"user_id": userId},
    );
    if (object.code == XinDongServerStatus.success) {
      List list = object.data;
      return list.map((e) => XinDongAuthClass.fromJson(e)).toList();
    }
    return [];
  }

  // 拉黑
  Future<bool> blackListAdd({required dynamic userId}) async {
    final object = await XinDongNetworking.instance.request(
      XinDongApi.blackListAdd,
      method: XinDongHttpMethod.post,
      parameters: {"to_user": userId},
    );
    return object.code == XinDongServerStatus.success;
  }

  // 关注/取消关注
  Future<bool> attention({required dynamic userId}) async {
    final object = await XinDongNetworking.instance.request(
      XinDongApi.attention,
      method: XinDongHttpMethod.post,
      parameters: {"to_user": userId},
    );
    return object.code == XinDongServerStatus.success;
  }
}
